<template>
  <ion-page>
    <ion-content>
      <div class="bg">
        <img src="@/assets/images/logo.png" alt="">
        <p>欢迎使用艺考刷题宝</p>
      </div>
      <div class="forms">
        <ion-list padding-left padding-right>
          <ion-item no-padding margin-bottom>
            <ion-input type="text" value="" placeholder="请输入手机号" v-model="loginInfo.phone">
            </ion-input>
          </ion-item>
          <ion-item no-padding margin-bottom>
            <ion-input type="password" value="" placeholder="请输入密码" v-model="loginInfo.password">
            </ion-input>
          </ion-item>
        </ion-list>
        <p><ion-checkbox @ionChange="change()" label-placement="end" :checked="loginInfo.checked" color="danger"></ion-checkbox>我已阅读<span @click="setOpen($event)">《用户服务协议》</span> 和<span @click="setOpen">《隐私政策》</span></p>
        <div padding class="but">
          <button ion-button block @click="login()" :disabled="!loginInfo.checked">登录</button>
        </div>
        <ion-row>
            <ion-col>
              没有账号？<span @click="register(1)">立即注册</span>
            </ion-col>
            <ion-col class="text-right" @click="register(2)">
              忘记密码
            </ion-col>
          </ion-row>
      </div>
    </ion-content>

  </ion-page>
</template>

<script lang="ts">
import { IonIcon, IonPage, IonContent, IonRow, IonCol, IonInput, IonItem, IonList, alertController } from '@ionic/vue';
import { personOutline, lockClosedOutline } from 'ionicons/icons';
import { reactive, toRefs, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { PublicService } from '@/utils/public';
const publicService = new PublicService();
export default {
  name: 'login-page',
  components: {
    IonPage, IonContent, IonRow, IonCol, IonInput, IonItem, IonList,
  },
  setup() {
    const router = useRouter();
    const state = reactive({
      loginInfo: {
        phone: '', // 用户名
        password: '',  // 密码
        remberName: false,
        remberPwd: false,
        checked: false
      }
    })

    function change(){
      state.loginInfo.checked = !state.loginInfo.checked;
    }

    function register(type: number){
      // register
      router.push({
        path: '/register',
        query: {
          type
        }
      });
    }

    const setOpen = async (ev: any) => {
      ev.stopPropagation();
      const alert = await alertController.create({
        header: '用户隐私协议',
        message: '温馨提示<br />11',
        id: 'thsPromptAlert',
        htmlAttributes: {
          'aria-label': 'alert dialog',
        },
        buttons: [
          {
            text: '不同意',
            role: 'cancel',
            cssClass: 'alert-button-cancel',
            handler: () => {
              state.loginInfo.checked = false;
            },
          },
          {
            text: '同意并继续',
            role: 'confirm',
            cssClass: 'alert-button-confirm',
            handler: () => {
              state.loginInfo.checked = true;
            },
          },
        ],
      });

      await alert.present();
    };


    return {
      ...toRefs(state),
      setOpen,
      personOutline, 
      lockClosedOutline,
      change,
      register
    }
  }
}
</script>
<style lang="scss" scoped>
.bg{
  img{
    width: 30%;
    display: block;
    margin: 100px auto 20px;
  }
  p{
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 100px;
  }
}
.forms {
  width: 90%;
  background: #fff;
  border-radius: 12px;
  margin: 150px auto;
  position: relative;
  top: -111px;
  padding: 35px 24px;
  p{
    color: #939699;
    font-size: 14px;
    text-align: center;
    span{
      color: #CA3032;
    }
  }
  .l-icon {
    position: absolute;
    font-size: 1.5em;
    color: #696969;
    margin-left: 10px;
    top: 10px;
    left: -35px;
  }

  ion-item {
    --padding-start: 0px;
    --padding-end: 0px;
    --inner-padding-end: 0px;
    --inner-border-width: 0px;
    margin-bottom: 15px;
  }

  ion-input {
    position: relative;
    font-size: 14px;
    border-bottom: 1px solid #f4f4f4;
  }

  ion-row {
    font-size: 14px;
    margin-bottom: 20px;
    .text-right {
      text-align: right;
    }
    ion-col{
      color: #939699;
      span{
        color: #CA3032;
      }
    }

    img {
      width: 14px;
      height: 14px;
      position: relative;
      top: 2px;
    }
  }
  ion-checkbox{
    --size: 15px;
    display: inline-block;
    vertical-align: bottom;
    margin-right: 10px;
    position: relative;
    top: -2px;
  }

  button {
    width: 100%;
    height: 40px;
    border-radius: 10px;
    color: #fff;
    background: #CA3032;
    margin-bottom: 10px;
    img{
      width: 20px;
      margin-right: 5px;
      vertical-align: middle;
    }
  }
}
button.alert-button.alert-button-confirm {
  background-color: #CA3032;
  color: var(--ion-color-success-contrast);
}
button.alert-button.alert-button-cancel{
  color: #333;
}
</style>